/**
 * 导入模块
 */
import AxiosRequest from '@/http/axios/index';
import interceptor from './interceptor';
import getRequsetBaseUrl from './url';

/**
 * 创建一个axios实例
 */
const request: AxiosRequest = new AxiosRequest({
  /**
   * 超时时间
   */
  timeout: 20 * 1000,
  /**
   * 拦截器
   */
  interceptorHooks: interceptor,
});

/**
 * 定义请求类
 */
class Service {
  /**
   * get请求
   * @param {*} url
   * @param {*} params
   */
  public static get<T = any>(url: string, params: RequestParams) {
    let apiUrl: string = getRequsetBaseUrl(url);
    let data: Record<string, any> = {
      params: params,
    };
    return request.get<ResponseServer<T>>(apiUrl, data);
  }

  /**
   * post请求
   * @param {*} url
   * @param {*} params
   */
  public static post<T = any>(url: string, params: RequestParams, config?: any) {
    let apiUrl: string = getRequsetBaseUrl(url);
    return request.post<ResponseServer<T>>(apiUrl, params, config);
  }

  /**
   * put请求
   * @param {*} url
   * @param {*} params
   */
  public static put<T = any>(url: string, params: RequestParams, config?: any) {
    let apiUrl: string = getRequsetBaseUrl(url);
    return request.put<ResponseServer<T>>(apiUrl, params, config);
  }

  /**
   * delete请求
   * @param {*} url
   * @param {*} params
   */
  public static delete<T = any>(url: string, params: RequestParams) {
    let apiUrl: string = getRequsetBaseUrl(url);
    let data: Record<string, any> = {
      params: params,
    };
    return request.delete<ResponseServer<T>>(apiUrl, data);
  }

  /**
   * 自定义请求
   * @param url
   * @param method
   * @param config
   * @returns
   */
  public static request<T = any>(url: string, method: string, config: any) {
    let apiUrl: string = getRequsetBaseUrl(url);
    let params = {
      url: apiUrl,
      method: method,
      ...config,
    };
    return request.request<ResponseServer<T>>(params);
  }
}

/**
 * 导出服务
 */
export default Service;
